package com.demo.shiyan3.web.client

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageInfo;

import com.demo.shiyan3.model.domain.Article;
import com.demo.shiyan3.model.domain.Comment;
import com.demo.shiyan3.service.IArticleService;
import com.demo.shiyan3.service.ICommentService;
import com.demo.shiyan3.service.ISiteService;
import com.demo.shiyan3.service.Impl.ArticleServiceImpl;

@Controller
class IndexController {
    @Autowired
    private val articleServiceImpl: IArticleService? = null

    // 博客首页，会自动跳转到文章页
    @GetMapping(value = ["/"])
    private fun index(request: HttpServletRequest): String {
        return this.index(request, 1, 5)
    }

    // 文章页
    @GetMapping(value = ["/page/{p}"])
    fun index(request: HttpServletRequest, @PathVariable("p") page: Int, @RequestParam(value = "count", defaultValue = "5") count: Int): String {
        val articles = articleServiceImpl!!.selectArticleWithPage(page, count)
        // 获取文章热度统计信息
        val articleList = articleServiceImpl.heatArticles
        request.setAttribute("articles", articles)
        request.setAttribute("articleList", articleList)
        logger.info("分页获取文章信息: 页码 $page,条数 $count")
        return "client/index"
    }

    @Autowired
    private val commentServiceImpl: ICommentService? = null

    @Autowired
    private val siteServiceImpl: ISiteService? = null

    // 文章详情查询
    @GetMapping(value = ["/article/{id}"])
    fun getArticleById(@PathVariable("id") id: Int, request: HttpServletRequest): String {
        val article: Article = articleServiceImpl!!.selectArticleWithId(id);
        if (article != null) {
            // 查询封装评论相关数据
            getArticleComments(request, article)
            // 更新文章点击量
            siteServiceImpl!!.updateStatistics(article)
            request.setAttribute("article", article)
            return "client/articleDetails"
        } else {
            logger.warn("查询文章详情结果为空，查询文章id: $id")
            // 未找到对应文章页面，跳转到提示页
            return "comm/error_404"
        }
    }




    // 查询文章的评论信息，并补充到文章详情里面
    private fun getArticleComments(request: HttpServletRequest, article: Article) {
        if (article.allowComment) {
            // cp表示评论页码，commentPage
            var cp = request.getParameter("cp")
            cp = if (StringUtils.isBlank(cp)) "1" else cp
            request.setAttribute("cp", cp)
            val comments: PageInfo<Comment> = commentServiceImpl!!.getComments(article.id, cp.toInt(), 3)
            request.setAttribute("cp", cp)
            request.setAttribute("comments", comments)
        }
    }

    companion object {
        private val logger: Logger = LoggerFactory.getLogger(IndexController::class.java)
    }
}